/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.grt192.demos;

import com.grt192.core.CommandArray;
import com.grt192.core.GRTObject;
import com.grt192.spot.actuator.GRTDemoLED;
import com.grt192.spot.mechanism.hauntedHouse.StatusLight;
import com.grt192.utils.Util;
import com.sun.spot.resources.transducers.ILightSensor;
import com.sun.spot.sensorboard.EDemoBoard;
import java.io.IOException;

/**
 * Demos non-pid control of attempting to have a certain room brightness
 * @author ajc
 */
public class LightFeedbackDemo extends GRTObject {

    public LightFeedbackDemo() {
        final double LIGHT_STEP = 25;
        final int DESIRED_LIGHT = 50;
        ILightSensor lightSensor = EDemoBoard.getInstance().getLightSensor();
        StatusLight status = new StatusLight(new GRTDemoLED(0));
        status.rawColor(new CommandArray(255, 255, 255));
        Util.sleep(5000);
        int currentLight = 0;

        while (true) {

            int light;
            try {
                light = lightSensor.getAverageValue();
            } catch (IOException ex) {
                light = 0;
                ex.printStackTrace();
            }
            log("Light \t" + light);

            if (light < DESIRED_LIGHT) {// too dark, brighten up
                currentLight += LIGHT_STEP;
            } else if (light > DESIRED_LIGHT) {//to bright, dim down
                currentLight -= LIGHT_STEP;
            }

            //limit color command: don't go brighter than 255 or less than 0.
            if (currentLight > 255) {
                currentLight = 255;
            } else if (currentLight < 0) {
                currentLight = 0;
            }

            log("Current \t" + currentLight);
            status.rawColor(new CommandArray(currentLight, currentLight, currentLight));
            Util.sleep(150);

        }
    }
}
